package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.SystemDictionaryService;
import cn.wolfcode.crm.utils.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {
    @Autowired
    private SystemDictionaryService systemDictionaryService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"systemDictionaryController:list","字典目录列表"},logical = Logical.OR)
    public String list(Model model , @ModelAttribute("qo" )QueryObject qo) {
        PageInfo<SystemDictionary> pageInfo = systemDictionaryService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "/systemDictionary/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"systemDictionaryController:delete","字典目录删除"},logical = Logical.OR)
    public JsonResult delete(Long id) {
       try {
           systemDictionaryService.deleteByPrimaryKey(id);
       }catch (Exception e){
           return new JsonResult(false,"删除失败");

       }
        return new JsonResult(true,"删除成功");



    }
 /*   @RequestMapping("/input")
    @RequiresPermissions(value = {"systemDictionaryController:input","字典目录编辑"},logical = Logical.OR)
    public String input(Model model,Long id) {
        if (id != null) {
            SystemDictionary systemDictionary = systemDictionaryService.selectByPrimaryKey(id);
            model.addAttribute("systemDictionary",systemDictionary);
        }
        return "/systemDictionary/input";

    }*/
    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"systemDictionaryController:saveOrUpdate","字典目录新增或修改"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionary systemDictionary) {

        try {
           if (systemDictionary.getId()!=null) {
               systemDictionaryService.updateByPrimaryKey(systemDictionary);
           }else {
               systemDictionaryService.insert(systemDictionary);
           }
            //int i=1/0;
       }catch (Exception e){
           return new JsonResult(false,"保存或者修改失败");
       }
        return new JsonResult(true,"保存或者修改成功");





    }


}
